from syslogd.log.Filter import Filters, Filter
import time
from syslogd.gui.LogReader import LogReader


class Stats(object):
    
    instance = None

    def __init__(self, filters):
        self.filters = filters
        Stats.instance=self
        

    def getStats(self):
        stats = ''
        stats += "**********************************************************************\n"
        stats += "***********************Syslogs statistics ****************************\n"
        stats += "**********************************************************************\n\n"       
        
        logs = self.filters.getLogs()
        if len(logs)>1:
            first = logs[1]
            last = logs[len(logs)-1]
            
            stats += "\tLogs from start application: " + str(self.filters.getLoggerSize()) + "\n"
            stats += "\t\tFirst log time: " + str(first.getTime()) + "\n"
            stats += "\t\tLast log time: " + str(last.getTime()) + "\n"
            interval = float(time.time())-float(first.getTime())
            if interval == 0.0:
                interval = float(1)
            stats += "\t\tLogs per second: " + str(float(len(logs))/interval) + "\n\n"
            
        stats += "\tFilters count: " + str(self.filters.getFiltersSize()) + "\n"
        for fl in self.filters.getFiltersKeys():
            stats += "\t\t" + fl.__str__() + "\n"
        
        stats += "**********************************************************************\n"  
        stats += "**********************************************************************\n\n" 
        return stats
    
    @staticmethod
    def getInstance():
        if Stats.instance is None:
            filters = LogReader.filters
            filters.addFilter(Filter([".*in.*","s"],20,4))
            Stats(filters)
        return Stats.instance
